package com.czxy.demo26;

public class MyTask implements Runnable {
    // 票数100张   打印 “X号窗口出票成功，现在余票X张”
    private int num=100;
    // 创建一个锁对象
    Object obj=new Object();

    @Override
    public void run() {
        // 卖票
        while (true) {
            //同步代码块
            synchronized (obj){
                if (num>0){
                    System.out.println(Thread.currentThread().getName()+"窗口出票成功，票号"+num);
                    num--;
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    break;
                }
            }
        }
    }
}